import { app, BrowserWindow } from 'electron'
import nuxtServer from './nuxtServer'
import fs from 'fs'
import path from 'path'
import koaServer from './server'

const getNuxtConfig = fs.existsSync(path.resolve(__dirname, 'nuxt.config.js'))
  ? import(path.resolve(__dirname, 'nuxt.config'))
  : import(path.resolve(__dirname, '../../nuxt.config'))
function createWindow() {
  // 创建浏览器窗口
  return new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
    },
  })
}

app.whenReady().then(async () => {
  const nuxtConfig = (await getNuxtConfig).default
  if (nuxtConfig.target === 'server') {
    await nuxtServer()
  }
  koaServer()
  const win = createWindow()
  // 加载index.html文件
  if (nuxtConfig.target === 'server') {
    win.loadURL('http://127.0.0.1:7001')
  } else {
    win.loadURL('http://127.0.0.1:7002')
  }
})
